package com.abbas.myviewtest.view.utils;

import android.util.Log;

import com.abbas.myviewtest.view.listener.OnViewGeneratorListener;

import java.util.Random;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/3/31 10:24
 */
public class ViewGenerator {

    private final int LEFT = 0;
    private final int RIGHT = 1;
    private final int UP = 2;
    private final int DOWN = 3;

    private int[][] childViewNumber;
    private int rowSize;
    private int columnSize;
    private int childViewCount;

    private int blankViewX;
    private int blankViewY;

    private OnViewGeneratorListener generatorListener;

    public ViewGenerator(int rowSize, int columnSize,OnViewGeneratorListener generatorListener) {
        this.rowSize = rowSize;
        this.columnSize = columnSize;
        this.generatorListener = generatorListener;
        startGenerator();
    }

    public void startGenerator(){
        //初始化数组
        childViewNumber = new int[rowSize][columnSize];
        //初始化子视图总数
        childViewCount = rowSize * columnSize;

        //为视图数组赋值
        int temp = 1;
        for (int i = 0; i < rowSize; i++) {
            for (int i1 = 0; i1 < columnSize; i1++) {
                childViewNumber[i][i1] = temp;
                temp++;
            }
        }

        //记录空白位置
        blankViewX = rowSize - 1;
        blankViewY = columnSize - 1;

        //开始打乱回调
        if (generatorListener != null){
            generatorListener.onStartGenerate();
        }

        //开始打乱
        for (int i = 0; i < 100; i++) {
            toRandomlyMove();
        }
        //将空白位置移动至右下角
        while (blankViewY != columnSize - 1){
            moveToDown(blankViewX,blankViewY);
            blankViewY++;
        }
        while (blankViewX != rowSize - 1){
            moveToRight(blankViewX,blankViewY);
            blankViewX++;
        }
        //移动完毕回调
        if (generatorListener != null) generatorListener.onGenerated(childViewNumber);
    }

    private void toRandomlyMove() {
        switch (RandomUtils.randomIntFromOToFour()){
            case LEFT:
                if (moveToLeft(blankViewX,blankViewY)){
                    blankViewX--;
                }
                break;
            case RIGHT:
                if (moveToRight(blankViewX,blankViewY)){
                    blankViewX++;
                }
                break;
            case UP:
                if (moveToUp(blankViewX,blankViewY)){
                    blankViewY--;
                }
                break;
            case DOWN:
                if (moveToDown(blankViewX,blankViewY)){
                    blankViewY++;
                }
                break;
        }
    }

    private boolean moveToDown(int blankViewX, int blankViewY) {
        if (blankViewY < columnSize - 1){
            Log.e("moveToDown","moveToDown");
            exChange(blankViewX,blankViewY,blankViewX,blankViewY+1);
            return true;
        }
        return false;
    }

    private boolean moveToUp(int blankViewX, int blankViewY) {
        if (blankViewY > 0){
            Log.e("moveToDown","moveToDown");
            exChange(blankViewX,blankViewY,blankViewX,blankViewY-1);
            return true;
        }
        return false;
    }

    private boolean moveToRight(int blankViewX, int blankViewY) {
        if (blankViewX < columnSize - 1){
            Log.e("moveToDown","moveToDown");
            exChange(blankViewX,blankViewY,blankViewX+1,blankViewY);
            return true;
        }
        return false;
    }

    private boolean moveToLeft(int blankViewX, int blankViewY) {
        if (blankViewX > 0){
            Log.e("moveToLeft","moveToLeft");
            exChange(blankViewX,blankViewY,blankViewX-1,blankViewY);
            return true;
        }
        return false;
    }

    /**
     * @author abbas
     * @date 2022/3/31 10:55
     * @description: 交换位置
     */
    private void exChange(int blankViewX, int blankViewY, int changeViewX, int changeViewY) {
        Log.e("ex",blankViewX + " " + blankViewY);
        //拿到空白位置上的数
        int temp = childViewNumber[blankViewX][blankViewY];
        //将空白位置上的数变成移动的view的数字
        childViewNumber[blankViewX][blankViewY] = childViewNumber[changeViewX][changeViewY];
        //将移动后位置上的数字设置为空白位置上的数字
        childViewNumber[changeViewX][changeViewY] = temp;
    }


    /**
     * @author abbas
     * @date 2022/3/31 10:45
     * @description: 获取0到4的随机数
     */
    static class RandomUtils{
        static Random random = new Random();

        static int randomInt(int from,int to){
            return random.nextInt(to-from);
        }

        static int randomIntFromOToFour(){
            return random.nextInt(4);
        }
    }


}
